﻿using System;
using System.Configuration;
using Castle.ActiveRecord;
using Castle.ActiveRecord.Framework.Config;
using ModelGenerator.ModelGenerator;
using ModelGenerator.RepositoryGenerator;
using DataBaseGeneration.Schema;

namespace DataBaseGeneration
{
    internal class Program
    {
        private static void Main()
        {
            // Generate the db or the scripts
            GenerateDb();

            // Move the model
            ModelMover.moveModel(typeof (Customer));

            // Generate the repository
            RepositoryGenerator.GenerateRepositories(typeof (Library.Core.Model.Customer));
        }

        /// <summary>
        /// Generates the db.
        /// </summary>
        private static void GenerateDb()
        {
            var source = new XmlConfigurationSource("ActiveRecordConfiguration.xml");
            ActiveRecordStarter.Initialize(typeof (Customer).Assembly, source);

            string TargetSchemaDir = ConfigurationManager.AppSettings["TargetSchemaDir"];
            bool generateDb = Convert.ToBoolean(ConfigurationManager.AppSettings["generateDb"]);

            if (generateDb)
                ActiveRecordStarter.CreateSchema();
            ActiveRecordStarter.GenerateCreationScripts(string.Format(@"{0}\CreateDB.sql", TargetSchemaDir));
            ActiveRecordStarter.GenerateDropScripts(string.Format(@"{0}\DropDB.sql", TargetSchemaDir));
        }
    }
}